#ifndef LED_MATRIX_DISPLAY_H
#define LED_MATRIX_DISPLAY_H

#pragma once
#include <FastLED.h>
#include "DisplayLayout.h"

template <uint8_t PIN>
class LEDMatrixDisplay {
public:
    LEDMatrixDisplay(uint16_t width, uint16_t height) 
        : LED_WIDTH(width), LED_HEIGHT(height), NUM_LEDS(width * height), layout(width, height) 
    {
        leds = new CRGB[NUM_LEDS];
        FastLED.addLeds<WS2812, PIN, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
    }

    ~LEDMatrixDisplay() {
        delete[] leds;
    }

    void drawLetter(CRGB* leds, uint8_t x, uint8_t y, uint8_t letter, CRGB color) {
        layout.drawDigit(leds, x, y, letter, color);
    }
    
    void begin() {
        FastLED.clear();
        FastLED.show();
    }

    void clear() { 
        FastLED.clear(); 
    }

    void show() { 
        FastLED.show(); 
    }

    void setBrightness(uint8_t brightness) { 
        FastLED.setBrightness(brightness); 
    }

    void displayTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint8_t dayOfWeek) {
        clear();
        animateColons();
        
        uint8_t timeX = layout.getTimePositionX();
        uint8_t timeY = layout.getTimePositionY();
        
        // 小时显示
        layout.drawDigit(leds, timeX, timeY, hours / 10, hourColor);
        layout.drawDigit(leds, timeX + 4, timeY, hours % 10, hourColor);
        
        // 第一个冒号
        if (colonState) {
            layout.drawColon(leds, timeX + 8, timeY, colonColor);
        }
        
        // 分钟显示
        layout.drawDigit(leds, timeX + 10, timeY, minutes / 10, minColor);
        layout.drawDigit(leds, timeX + 14, timeY, minutes % 10, minColor);
        
        // 第二个冒号
        if (colonState) {
            layout.drawColon(leds, timeX + 18, timeY, colonColor);
        }
        
        // 秒显示
        layout.drawDigit(leds, timeX + 20, timeY, seconds / 10, secColor);
        layout.drawDigit(leds, timeX + 24, timeY, seconds % 10, secColor);
        
        // 在底部绘制星期指示器
        layout.drawWeekIndicator(leds, dayOfWeek, weekActiveColor, weekInactiveColor);
        
        // 立即显示
        FastLED.show();
    }

    void setTimeColor(CRGB hourColor, CRGB minColor, CRGB secColor) {
        this->hourColor = hourColor;
        this->minColor = minColor;
        this->secColor = secColor;
    }

    void setColonColor(CRGB colonColor) {
        this->colonColor = colonColor;
    }

    void setDateColor(CRGB dateColor) {
        this->dateColor = dateColor;
    }

    void animateColons() {
        unsigned long currentMillis = millis();
        if (currentMillis - lastColonBlink > colonBlinkInterval) {
            colonState = !colonState;
            lastColonBlink = currentMillis;
        }
    }

    void setWeekColors(CRGB activeColor, CRGB inactiveColor) {
        this->weekActiveColor = activeColor;
        this->weekInactiveColor = inactiveColor;
    }

    void drawDate(uint8_t month, uint8_t day, uint8_t dayOfWeek, CRGB color) {
        clear();
        uint8_t dateX = layout.getDatePositionX();
        uint8_t dateY = layout.getDatePositionY();
        
        // 绘制日历图标
        layout.drawCalendarIcon(leds, dateX, dateY, color);
        
        // 月份十位
        layout.drawSmallDigit(leds, dateX + 5, dateY, month / 10, color);
        layout.drawSmallDigit(leds, dateX + 9, dateY, month % 10, color);
        
        // 分隔符
        int sepX = dateX + 13;
        if (sepX < layout.getWidth() && dateY + 2 < layout.getHeight()) {
            leds[layout.getLedIndex(sepX, dateY + 2)] = color;
            if (sepX + 1 < layout.getWidth()) {
                leds[layout.getLedIndex(sepX + 1, dateY + 2)] = color;
            }
        }
        
        // 日期十位
        layout.drawSmallDigit(leds, dateX + 16, dateY, day / 10, color);
        layout.drawSmallDigit(leds, dateX + 20, dateY, day % 10, color);
        
        // 在底部绘制星期指示器
        layout.drawWeekIndicator(leds, dayOfWeek, weekActiveColor, weekInactiveColor);
        
        FastLED.show();
    }

    void displayError() {
        static uint8_t hue = 0;
        static uint8_t offset = 0;
        
        clear();
        
        // 创建移动的彩虹效果
        for (uint8_t x = 0; x < LED_WIDTH; x++) {
            for (uint8_t y = 0; y < LED_HEIGHT; y++) {
                uint8_t pixelHue = hue + (x * 10) + (y * 10) + offset;
                int index = layout.getLedIndex(x, y);
                if (index >= 0 && index < NUM_LEDS) {
                    leds[index] = CHSV(pixelHue, 255, 255);
                }
            }
        }
        
        hue += 2;
        offset += 5;
        FastLED.show();
    }

    void fillScreen(CRGB color) {
        fill_solid(leds, NUM_LEDS, color);
        FastLED.show();
    }

    void testDrawDigit(uint8_t x, uint8_t y, uint8_t num, CRGB color) {
        layout.drawDigit(leds, x, y, num, color);
        FastLED.show();
    }

    void displayAmbient() {
        static uint8_t hue = 0;
        hue += 2;
        
        for (uint8_t x = 0; x < LED_WIDTH; x++) {
            uint8_t pixelHue = hue + (x * 10);
            for (uint8_t y = 0; y < LED_HEIGHT; y++) {
                int index = layout.getLedIndex(x, y);
                if (index >= 0 && index < NUM_LEDS) {
                    leds[index] = CHSV(pixelHue, 255, 255);
                }
            }
        }
        FastLED.show();
    }

    DisplayLayout& getLayout() { return layout; }
    CRGB getDateColor() { return dateColor; }
    CRGB* getLeds() { return leds; }

private:
    CRGB* leds;
    uint16_t LED_WIDTH;
    uint16_t LED_HEIGHT;
    uint16_t NUM_LEDS;
    DisplayLayout layout;
    
    // 颜色配置
    CRGB hourColor = CRGB(0, 100, 255);
    CRGB minColor = CRGB(0, 255, 100);
    CRGB secColor = CRGB(255, 200, 0);
    CRGB colonColor = CRGB(255, 50, 50);
    CRGB dateColor = CRGB(180, 0, 255);
    CRGB weekActiveColor = CRGB(255, 0, 0);
    CRGB weekInactiveColor = CRGB(50, 50, 50);
    
    // 动画状态
    bool colonState = true;
    unsigned long lastColonBlink = 0;
    const uint16_t colonBlinkInterval = 500;
};

#endif